package org.example;

import org.springframework.stereotype.Service;

import java.util.*;

import java.util.*;

@org.springframework.stereotype.Service
public class ComodityService {


    private Map<Long, ComodityItem> comodities =
            Collections.synchronizedMap(new HashMap<Long, ComodityItem>());

    public ComodityItem addComodity(ComodityItem comodity) {
        comodities.put(comodity.getId(), comodity);
        return comodity;
    }

    public ComodityItem getComodity(long id) {
        return comodities.get(id);
    }

    public List<ComodityItem> findComodity(String name, Boolean complete) {
        List<ComodityItem> result=new ArrayList<>();
        for (ComodityItem comodity: comodities.values()){
            if (name!=null && !comodity.getName().contains(name)) {
                continue;
            }
            if (complete!=null && !complete.equals(comodity.isComplete())) {
                continue;
            }
            result.add(comodity);
        }
        return result;
    }

    public void updateComodity(long id, ComodityItem comodity) {
        ComodityItem comodity2  = comodities.get(id);
        comodity2.setName(comodity.getName());
        comodity2.setComplete(comodity.isComplete());
        comodities.put(id, comodity2);
    }

    public void deleteComodity(long id) {
        comodities.remove(id);
    }

    public void deleteAll(){ comodities.clear();
    }


}
